package com.zplus.managers.petservices.services.impls;

import com.zplus.managers.petservices.entities.Pet;
import com.zplus.managers.petservices.mappers.PetMapper;
import com.zplus.managers.petservices.services.PetService;
import com.zplus.managers.petservices.services.PetState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PetServiceImpl implements PetService
{
    @Autowired
    private PetMapper petMapper;
    @Override
    public boolean newPet(Pet pet)
    {
        // check essential field
        if(pet.getPetname().equals("")&& pet.getSpecies().equals("") && pet.getGender().equals("")&&
        pet.getBirthday() == null)
            return false;

        // set origin state
        pet.setState(0);
        return petMapper.insertPet(pet) > 0;
    }

    @Override
    public List<Pet> queryPets(PetState state)
    {

        List<Pet> pets = null;
        if(state == PetState.ALL)
            // query all pet
            pets = petMapper.queryAllPet();
        else
            pets = petMapper.queryAllPetByState(state.ordinal());

        if (pets == null||pets.isEmpty())
            return null;
        else
            return pets;
    }

    @Override
    public Pet queryPetById(int id)
    {
        return petMapper.queryPetById(id);
    }

    @Override
    public boolean updatePetInfo(Pet petAft)
    {
        if(petAft.getPetname().equals("")&& petAft.getSpecies().equals("") && petAft.getGender().equals("")&&
                petAft.getBirthday() == null)
            return false;
        return petMapper.updatePetInfo(petAft) > 0;
    }

    @Override
    public boolean deletePet(Pet pet)
    {
        int i = petMapper.dropPet(pet.getId());
        return i > 0;
    }

    @Override
    public List<Pet> getPetsBySpeci(String speci)
    {
        List<Pet> pets = petMapper.queryPetsBySpeci(speci);
        return pets;
    }

}
